<?php

class SlotDB {

    public static function getSlotByID($slot_id) {//works
        try {
            $query = "SELECT * FROM slot WHERE slot_id = :slot_id";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            $stmt->bindParam(':slot_id', $slot_id);
            $stmt->execute();
            $rowsAffect = $stmt->rowCount();
            if ($rowsAffect > 0) {
                $result = $stmt->fetch(PDO::FETCH_OBJ);
                return self::getSlotFromPDO($result);
            }
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }

    private static function getSlotFromPDO($result) {//works
        $slot = new Slot();
        $slot->setSlot_id($result->slot_id);
        $slot->setSlot_time_from($result->slot_time_from);
        $slot->setSlot_time_to($result->slot_time_to);
        return $slot;
    }

    public static function getAllSlots() {
        try {
            $query = "SELECT * FROM slot";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            $stmt->execute();
            $rowsAffected = $stmt->rowCount();
            if ($rowsAffected > 0) {
                $result = $stmt->fetchAll(PDO::FETCH_OBJ);
                $inventory = new Inventory();
                foreach ($result as $item) {
                    $slot = self::getSlotFromPDO($item);
                    $inventory->attach($slot, $slot->getSlot_id());
                }
                return $inventory;
            } else {
                return null;
            }
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }
    
    public static function getSlotsCount() {
        $query = "Select COUNT(*) as count From slot";
        $dbh = DatabaseConnection::getDB();
        $stmt = $dbh->prepare($query);    
        $stmt->execute();
        /* Fetch the first column from the next row in the result set */
        //print("Fetch the first column from the next row in the result set:\n");
        $count = $stmt->fetchColumn();
        return $count;
    }

}

?>
